﻿<!DOCTYPE html>
<html>
<head>
    <title>OAuth-Server</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <link href="http://g.tbcdn.cn/sj/wqui/1.0.0/css/sui.min.css" rel="stylesheet">
    <script type="text/javascript" src="http://g.tbcdn.cn/sj/lib/jquery/dist/jquery.min.js"></script>
    <script type="text/javascript" src="http://g.tbcdn.cn/sj/wqui/1.0.0/js/sui.min.js"></script>
</head>
<body>
    <div class="sui-container" style="margin-top:10px;margin-left:10px">
        <h2>使用OAuth2.0登录</h2>
        <div class="sui-msg msg-large msg-block msg-success" style="display:none">
            <div class="msg-con">
                <label></label>
                <a id="authorize" style="cursor:pointer">授权</a>
            </div>
            <s class="msg-icon"></s>
        </div>
        <div id="loginForm">
            <form class="sui-form form-horizontal" autocomplete="off">
                <div class="control-group">
                    <label for="inputEmail" class="control-label">用户名：</label>
                    <div class="controls">
                        <input type="text" id="inputUserName" placeholder="用户名" class="input-block-level">
                    </div>
                </div>
                <div class="control-group">
                    <label for="inputPassword" class="control-label">密码：</label>
                    <div class="controls"><input type="text" id="inputPassword" placeholder="密码"></div>
                </div>
                <div class="control-group">
                    <label class="control-label"></label>
                    <div class="controls">
                        <button type="button" class="sui-btn btn-primary">登录</button>
                    </div>
                </div>
            </form>
        </div>

    </div>
    <script>
        $(function () {
            $('.sui-btn').on('click', function () {
                var username = $.trim($('#inputUserName').val());
                var userpwd = $.trim($('#inputPassword').val());
                if (username.length == 0 || userpwd.length == 0) {
                    alert("用户名或者密码没有填写");
                    return;
                }
                $(this).off('click');
                $.post('/do', { action: 'user_login', username: username, userpwd: userpwd, client: getQuery("client_id") }, function (res) {
                    if (res.error == 0) {
                        $('.msg-con label').html('hello:' + username);
                        $('.sui-msg').show();
                        $('#loginForm').hide();
                    } else {
                        alert("出错了，稍后再试");
                        location.href = location.href;
                    }
                });
            });
            $("#authorize").on('click', function () {
                $.post('/do', { action: 'user_grant', token: getQuery("token") }, function (res) {
                    if (res.error == 0 && res.data.grant) {
                        var link = window.location.href;
                        link = link.slice(link.indexOf('?') + 1);
                        link = link.slice(0, link.indexOf('token')-1);
                        window.location.href = "/oauth2/authorzie?response_type=" + res.data.type + "&" + link;
                    } else {
                        alert("出错了，稍后再试");
                        //location.href = location.href;
                    }
                });
            });
            function getQuery(para) {
                var reg = new RegExp("(^|&)" + para + "=([^&]*)(&|$)");
                var r = window.location.search.substr(1).match(reg);
                if (r != null) {
                    return unescape(r[2]);
                }
                return null;
            }
        });
    </script>
</body>
</html>